<?php

//OPTIONS API
//Define Defaults
function tsts_get_default_options() {
     $options = array(
          'header_nav_menu_position' => 'top',
          'display_footer_credit' => false,
          'skin' => 'blue',
          'theme_version' => '1.0'
     );
     return $options;
}

//Add Options to Database/Initialize Default Settings
function tsts_options_init() {
     // set options equal to defaults
     global $tsts_options;
     $tsts_options = get_option( 'tsts_options' );
     if ( false === $tsts_options ) {
          $tsts_options = tsts_get_default_options();
     }
     update_option( 'tsts_options', $tsts_options );
}
// Initialize Theme options
add_action('after_setup_theme','tsts_options_init', 9 );

//Helper Functions
function tsts_get_valid_skins() {
     $skins = array(
          'blue' => array(
               'slug' => 'blue',
               'name' => 'Blue',
               'description' => '"Blue" is a rather sad skin.'
          ),
          'red' => array(
               'slug' => 'red',
               'name' => 'Red',
               'description' => '"Red" is a rather angry skin.'
          ),
          'green' => array(
               'slug' => 'green',
               'name' => 'Green',
               'description' => '"Green" is a rather jealous skin.'
          )
     );
     return $skins;
}


//Settings Page Tabs:
function tsts_get_settings_page_tabs() {
     $tabs = array(
          'general' => 'General',
          'skins' => 'Skins'
     );
     return $tabs;
}


//Hook Settings Page Into Appearance Menu
// Add "tsts Options" link to the "Appearance" menu
function tsts_menu_options() {
     add_theme_page('TSTS Options', 'TSTS Options', 'edit_theme_options', 'tsts-settings', 'tsts_admin_options_page');
}
// Load the Admin Options page
add_action('admin_menu', 'tsts_menu_options');

//Create tabs
function tsts_admin_options_page_tabs( $current = 'general' ) {
	     if ( isset ( $_GET['tab'] ) ) :
          $current = $_GET['tab'];
     else:
          $current = 'general';
     endif;

    $tabs = tsts_get_settings_page_tabs();
    $links = array();
    foreach( $tabs as $tab => $name ) :
        if ( $tab == $current ) :
               $links[] = "<a class='nav-tab nav-tab-active' href='?page=tsts-settings&tab=$tab'>$name</a>";
          else :
               $links[] = "<a class='nav-tab' href='?page=tsts-settings&tab=$tab'>$name</a>";

        endif;
    endforeach;
     echo '<div id="icon-themes" class="icon32"><br /></div>';
     echo '<h2 class="nav-tab-wrapper">';

    foreach ( $links as $link )
        echo $link;
    echo '</h2>';
}






// Admin settings page markup
function tsts_admin_options_page() { ?>
     <div class="wrap">
          <?php tsts_admin_options_page_tabs(); ?>
          <?php if ( isset( $_GET['settings-updated'] ) ) { //lower than 3.1 change to 'updated' from 'settings-updated'
               echo "<div class='updated'><p>Theme settings updated successfully.</p></div>";
          } ?>
     <form action="options.php" method="post">
     <?php
     settings_fields('tsts_options');
     do_settings_sections('tsts');
     ?>
     <?php $tab = ( isset( $_GET['tab'] ) ? $_GET['tab'] : 'general' ); ?>
     <div class="buttons">
     <input name="tsts_options[submit-<?php echo $tab; ?>]" type="submit" class="button-primary" value="<?php esc_attr_e('Save Settings', 'tsts'); ?>" />
     <input name="tsts_options[reset-<?php echo $tab; ?>]" type="submit" class="button-secondary" value="<?php esc_attr_e('Reset Defaults', 'tsts'); ?>" />
     </form>
     </div>
     </div>
<?php }



//Register Settings and Define Form Sections/Fields
function tsts_reg_set() {
register_setting( 'tsts_options', 'tsts_options', 'tsts_options_validate' );
}
add_action( 'admin_init', 'tsts_reg_set' );

//Define Form Sections and Fields
//Separating Settings Per Tab
global $pagenow;
if ( 'themes.php' == $pagenow && isset( $_GET['page'] ) && 'tsts-settings' == $_GET['page'] ) :
     if ( isset ( $_GET['tab'] ) ) :
          $tab = $_GET['tab'];
     else:
          $tab = 'general';
     endif;
     switch ( $tab ) :
          case 'general' :
               require( THEME_OPTIONS . '/options-register-general.php' );
               break;
          case 'skins' :
               require( THEME_OPTIONS . '/options-register-skins.php' );
               break;
     endswitch;
endif;


//Enqueueing Admin CSS based on tab
function tsts_enqueue_admin_style() {
     // define admin stylesheet
     $admin_handle = 'tsts_admin_stylesheet';
     $admin_stylesheet = get_template_directory_uri() . '/lib/theme-options/options.css';

     wp_enqueue_style( $admin_handle, $admin_stylesheet );
}
// Enqueue Admin Stylesheet at admin_print_styles()
add_action('admin_print_styles-appearance_page_tsts-settings', 'tsts_enqueue_admin_style', 11 );





//Validate User-Input Form Data
function tsts_options_validate( $input ) {
     $tsts_options = get_option( 'tsts_options' );
     $valid_input = $tsts_options;

     // Determine which form action was submitted
     $submit_general = ( ! empty( $input['submit-general']) ? true : false );
     $reset_general = ( ! empty($input['reset-general']) ? true : false );
     $submit_skins = ( ! empty($input['submit-skins']) ? true : false );
     $reset_skins = ( ! empty($input['reset-skins']) ? true : false );

     if ( $submit_general ) { // if General Settings Submit
          $valid_input['header_nav_menu_position'] = ( 'below' == $input['header_nav_menu_position'] ? 'below' : 'above' );
          $valid_input['header_nav_menu_depth'] = ( ( 1 || 2 || 3 ) == $input['header_nav_menu_depth'] ? $input['header_nav_menu_depth'] : $valid_input['header_nav_menu_depth'] );
          $valid_input['display_footer_credit'] = ( 'true' == $input['display_footer_credit'] ? true : false );

     } elseif ( $reset_general ) { // if General Settings Reset Defaults
          $tsts_default_options = tsts_get_default_options();
          $valid_input['header_nav_menu_position'] = $tsts_default_options['header_nav_menu_position'];
          $valid_input['header_nav_menu_depth'] = $tsts_default_options['header_nav_menu_depth'];
          $valid_input['display_footer_credit'] = $tsts_default_options['display_footer_credit'];

     } elseif ( $submit_skins ) { // if skins Settings Submit
          $valid_skins = tsts_get_valid_skins();
          $valid_input['skin'] = ( array_key_exists( $input['skin'], $valid_skins ) ? $input['skin'] : $valid_input['skin'] );

     } elseif ( $reset_skins ) { // if skins Settings Reset Defaults
          $tsts_default_options = tsts_get_default_options();
          $valid_input['skin'] = $tsts_default_options['skin'];
     }
     return $valid_input;
}





//Contextual Help
function tsts_contextual_help() {

     $tsts_contextual_help_text = tsts_get_contextual_help_text();
     add_contextual_help( 'appearance_page_tsts-settings', $tsts_contextual_help_text  );
}
// Add contextual help to Admin Options page
add_action('admin_init', 'tsts_contextual_help', 10, 3);
function tsts_get_contextual_help_text() {

     $tabtext = '';
     require( THEME_OPTIONS . '/options-help.php' );
     return $tabtext;
}

/* EXAMPLE OF CALL FOR OPTION
$tsts_options = get_option( 'tsts_options' );
if ($tsts_options['display_footer_credit'] == 'true' ) { ?> 
TRUE <?php } else { ?> FALSE  <?php }
*/


?>